package org.xuyuji.study.sun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

/**
 * 这个FtpClient是sun内部使用的包，未开放，在JDK1.7中就不能使用了。
 * @author xuyuji
 *
 */
public class FTPDemo {
	public static void main(String[] args) throws IOException {
		Client client = new Client("192.168.1.112", 21, "xuyuji", "123");
//		client.download("/home/spepay/ftp/ums/00000000/20131112/48031000", "test_20131112_03.txt", "D:\\test_download.txt");
		client.upload("/home/spepay/ftp/ums/00000000/20131112/48031000", "test_upload.txt", "D:\\test_download.txt");
	}
}

class Client{
	
	private FtpClient ftpClient = new FtpClient();
	
	private String ip;
	
	private int port;
	
	private String loginName;
	
	private String loginPassword;
	
	private boolean isConnect;
	
	public Client(String ip, int port, String loginName, String loginPassword){
		this.ip = ip;
		this.port = port;
		this.loginName = loginName;
		this.loginPassword = loginPassword;
		isConnect = connect();
	}
	
	public boolean connect(){
		boolean flag = true;
		if(!ftpClient.serverIsOpen()){
			try {
				ftpClient.openServer(ip, port);
				ftpClient.login(loginName, loginPassword);
				System.out.println(ftpClient.getResponseString());
			} catch (IOException e) {
				flag = false;
				System.out.println("连接" + ip + ":" + port + "失败!" + e.getMessage());
			}
		}
		return flag;
	}
	
	private void close(){
		try {
			ftpClient.closeServer();
		} catch (IOException e) {
			System.out.println("关闭连接" + ip + ":" + port + "失败!" + e.getMessage());
		}
	}
	
	public void download(String remotePath, String remoteFileName, String localFilePath) throws IOException{
		if(isConnect){
			System.out.println("开始从" + remotePath + remoteFileName + "下载到" + localFilePath);
			ftpClient.cd(remotePath);
			TelnetInputStream in = ftpClient.get(remoteFileName);
			File localFile = new File(localFilePath + ".temp");
			FileOutputStream out = new FileOutputStream(localFile);
			byte[] buff = new byte[1024];
			int c;
			while( (c = in.read(buff)) != -1){
				out.write(buff, 0, c);
			}
			in.close();
			out.close();
			localFile.renameTo(new File(localFilePath));
			System.out.println("成功从" + remotePath + remoteFileName + "下载到" + localFilePath);
		}else{
			System.out.println("没有连接!");
		}
	}
	
	public void upload(String remotePath, String remoteFileName, String localFilePath) throws IOException{
		if(isConnect){
			System.out.println("开始从" + localFilePath + "上传到" + remotePath + remoteFileName);
			ftpClient.cd(remotePath);
			TelnetOutputStream out = ftpClient.put(remoteFileName + ".temp");
			FileInputStream in = new FileInputStream(localFilePath);
			byte[] buff = new byte[1024];
			int c;
			while( (c = in.read(buff)) != -1){
				out.write(buff, 0, c);
			}
			in.close();
			out.close();
			ftpClient.rename(remoteFileName + ".temp", remoteFileName);
			System.out.println("成功从" + localFilePath + "上传到" + remotePath + remoteFileName);
		}else{
			System.out.println("没有连接!");
		}
	}
}
